import 'dart:async';

main(List<String> args) {
  Future<String> getData() async {
    await Future.delayed(Duration(seconds: 5));
    return "返回一个Future对象";
  }

  pause(StreamSubscription streamSub) {
    if (streamSub != null && !streamSub.isPaused) {
      streamSub?.pause();
    }
  }

  cancel(StreamSubscription streamSub) {
    if (streamSub != null && !streamSub.isPaused) {
      streamSub?.cancel();
    }
  }

  resume(StreamSubscription streamSub) {
    if (streamSub != null && streamSub.isPaused) {
      streamSub.resume();
    }
  }
  // print(Stream<String>.fromFuture(getData()));
  StreamController<String> streamController =
      StreamController.broadcast(); //这个创建是异步的

  // StreamController<String> streamController = StreamController<String>(); //即使在第一个订阅被取消后，也无法在此类流上收听两次
  StreamSubscription sub;

  setListen() {
    sub = streamController.stream.listen((data) => print(data));
  }
  setListen();
  streamController.add('121');
  Future.delayed(Duration(seconds: 3), () {
    cancel(sub);
    streamController.sink.add('fasdf');
  });
  Future.delayed(Duration(seconds: 5), () {
    setListen();
    streamController.add('又开始监听了');
  });

  // streamController.add('fasfdasd');
  // resume(sub);
  // streamController.add('又开始监听了');
}
